package com.xbongbong.paas.help.factory.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.abstracts.AbstractOperatorFieldHandler;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.enums.ComboTypeEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.help.ProFormHelp;
import com.xbongbong.paas.help.factory.OperatorFieldHandler;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.ItemPoJo;
import com.xbongbong.paas.pojo.OperationsPojo;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.util.ItemUtil;
import com.xbongbong.pro.businessrule.pojo.BusinessRuleInfoPojo;
import com.xbongbong.pro.form.pojo.RuleTargetPojo;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author 吴峰
 * @version v1.0
 * @date 2020/3/4 21:11
 * @since v1.0
 */
@Component
public class OperatorComboHandlerImpl extends AbstractOperatorFieldHandler {

    @Override
    public List<Integer> fieldType() {
        return Arrays.asList(FieldTypeEnum.COMBO.getType(), FieldTypeEnum.RADIO_GROUP.getType());
    }

    @Resource
    private ProFormHelp proFormHelp;



    @Override
    public Object handlerThisFormField(PaasFormDataEntity paasFormDataEntity, JSONObject data, FieldAttrEntity fieldAttrEntity, BusinessRuleInfoPojo businessRuleInfoPojo) throws XbbException {
        String corpid = businessRuleInfoPojo.getCorpid();
        if (Objects.equals(ComboTypeEnum.CUSTOM.getType(), fieldAttrEntity.getComboType())) {
            List<ItemPoJo> itemPoJos = proFormHelp.getItem(fieldAttrEntity, businessRuleInfoPojo.getBusinessType(), corpid, paasFormDataEntity.getFormId());
            fieldAttrEntity.setItems(itemPoJos);
        }
        return ItemUtil.getCustomItemList(fieldAttrEntity, data);
    }

    @Override
    public Object handlerTargetFormField(PaasFormDataEntity paasFormDataEntity, JSONObject data, FieldAttrEntity fieldAttrEntity, FieldAttrEntity targetField,
                                         BusinessRuleInfoPojo businessRuleInfoPojo, OperationsPojo operation) throws XbbException {
        Object object = businessRuleInfoPojo.getObject();
        if (Objects.isNull(object)) {
            return null;
        }
        String corpid = businessRuleInfoPojo.getCorpid();
        RuleTargetPojo ruleTargetPojo = businessRuleInfoPojo.getRuleTargetPojo();
        Integer businessType = ruleTargetPojo.getLinkBusinessType();
        List<ItemPoJo> itemPoJos = proFormHelp.getItem(targetField, businessType, corpid, ruleTargetPojo.getFormId());
        targetField.setItems(itemPoJos);
        return ItemUtil.getCustomItemValue(targetField, String.valueOf(object));
    }

    @Override
    public Object handlerFixedValue(FieldAttrEntity targetField, BusinessRuleInfoPojo businessRuleInfoPojo, OperationsPojo operation) throws XbbException {
        if (Objects.equals(ComboTypeEnum.CUSTOM.getType(), targetField.getComboType()) && Objects.nonNull(operation.getValue())) {
            Object object = operation.getValue();
            if (object instanceof String) {
                String corpid = businessRuleInfoPojo.getCorpid();
                RuleTargetPojo ruleTargetPojo = businessRuleInfoPojo.getRuleTargetPojo();
                List<ItemPoJo> itemPoJos = proFormHelp.getItem(targetField, ruleTargetPojo.getLinkBusinessType(), corpid, ruleTargetPojo.getFormId());
                targetField.setItems(itemPoJos);
                object = ItemUtil.getCustomItemValue(targetField, String.valueOf(object));
                if (Objects.isNull(object)) {
                    // 避免不存在的值去更新数据
                    return null;
                }
            } else if (object instanceof Map || object instanceof ItemPoJo) {
                ItemPoJo itemPoJo = JSON.parseObject(JSON.toJSONString(object), ItemPoJo.class);
                object = itemPoJo.getValue();
            }
            return object;
        }
        return operation.getValue();
    }
}
